package com.shuyun.hystrix.dynamic.archaius;

import com.netflix.config.PollResult;
import com.netflix.config.PolledConfigurationSource;
import com.shuyun.hystrix.dynamic.config.ConfigManager;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @description:
 * @author: shuai.chen@shuyun.com
 * @date: 2023/1/17 1:50 PM
 */
public class CustomCfgConfigurationSource implements PolledConfigurationSource {

    private final static String CONFIG_KEY_PREFIX = "hystrix";

    @Override
    public PollResult poll(boolean initial, Object checkPoint) throws Exception {
        Map<String, Object> map = load();
        return PollResult.createFull(map);
    }
    private Map<String, Object> load() throws Exception{
        Map<String, Object> map = new HashMap<>();

        Set<String> keys = ConfigManager.keys();
        for(String key : keys) {
            if(key.startsWith(CONFIG_KEY_PREFIX)) {
                map.put(key, ConfigManager.get(key));
            }
        }

        return map;
    }
}
